import { Vector3, BufferGeometry, CatmullRomCurve3, LineBasicMaterial, Line, Color, Group, BufferAttribute } from 'three';

//运动轨迹线 start end 表示轨迹线的七点和结束点
function FlytrackFun(start, end){
    const group = new Group();
    const length = start.clone().sub(end).length(); //起点和终点的距离
    const H = length * 0.1; //根据两点之间的距离动态设置弧线的高度
    const middle = new  Vector3(0, 0, 0); //初始化中心点
    middle.add(start).add(end).divideScalar(2);
    middle.z +=H; //调整轨迹线的高度方向

    const geometry = new BufferGeometry();
    const curve = new CatmullRomCurve3([
        start,
        middle,
        end
    ]);
    const points = curve.getSpacedPoints(100); //细分100
    geometry.setFromPoints(points);
    const material = new LineBasicMaterial({
        color:0x00aaaa
    });
    const line = new Line(geometry, material);
    line['flyTrackPoints'] = points; //自定义属性用于飞线动画
    return line;
}
function FlyLineFun(points){
    var index = 20; //取点索引位置
    var num = 10; //从曲线上获取点数量
    var points2 = points.slice(index, index + num); //从曲线上获取一段
    var geometry2 = new  BufferGeometry();
    geometry2.setFromPoints(points2);

    // 批量计算所有顶点颜色数据
    var posNum = points2.length - 2; //分界点黄色，两端和轨迹线一个颜色青色
    var colorArr = [];
    for (var i = 0; i < points2.length; i++) {
        var color1 = new  Color(0x006666); //轨迹线颜色 青色
        var color2 = new  Color(0xffff00); //黄色
        var color !:null|any;
        // 飞线段里面颜色设置为黄色，两侧设置为青色，也就是说中间某个位置向两侧颜色渐变
        if (i < posNum) {
            color = color1.lerp(color2, i / posNum);
        } else {
            color = color2.lerp(color1, (i - posNum) / (points2.length - posNum));
        }
        (<any>colorArr).push(color.r, color.g, color.b);
    }
    // 设置几何体顶点颜色数据
    geometry2.attributes.color = new  BufferAttribute(new Float32Array(colorArr), 3);


    var material2 = new  LineBasicMaterial({
        linewidth:2.5
    });
    material2.vertexColors = true;
    //线条模型对象
    var line2 = new Line(geometry2, material2);
    line2['index'] = index*Math.random();
    line2['num'] = num;
    return line2;
}
export { FlytrackFun, FlyLineFun };